#coding=utf-8
'''
Created on 2013年9月4日

@author: xuben

跨服武斗管理器
'''

from ..common import Logger
from ..role import RoleData
import Kfwd
import KfwdData
import threading
import time

def run():
    if RoleData.inkfwd:
        t = threading.Thread(target = callback)
        t.start()

def callback():
    sleep_time = 5
    while True:
        Kfwd.getKfwdInfo()
        time.sleep(sleep_time)
        #下一次状态改变的秒数
        nextStateCd = int(KfwdData.nextStateCD / 1000)
        KfwdData.nextStateCD = 0
        #输出跨服状态
        kfwdStateStr = KfwdData.getKfwdStateStr()
        print kfwdStateStr
        Logger.log(kfwdStateStr)
        #比赛未开始
        if KfwdData.kfwdState < KfwdData.KFWD_STATE_ON:
            if nextStateCd > 0:
                sleep_time = nextStateCd
            else:
                sleep_time = 5
            continue
        #比赛已结束
        elif KfwdData.kfwdState > KfwdData.KFWD_STATE_ON:
            break
        #比赛进行中
        elif KfwdData.kfwdState == KfwdData.KFWD_STATE_ON:
            Kfwd.kfwdLogin()
            break
    
